
import Tkinter as tk
import math
# from PIL import Image, ImageTk
class MainClass: 
    
    # Called once when first created
    def __init__(self):
        self.window_width = 20   # Window properties
        self.window_height = 20
    
    # Draw the scene
    def draw(self):
        # Clear canvas
        self.w.delete("all")
        
        # Draw box
        #self.w.create_rectangle(0,0,self.window_width,self.window_height,fill="gray20")
        #self.canvas.create_image(0, 0, image = self.gif1, anchor = tk.NW)
    # Another function
    
    def Forces(self):
        self.name = "body"
        self.type = "circle"
        self.mass = 1

        self.position = [0.0,0.0,0.0]           # Position 
        self.velocity = [0.0,0.0,0.0]           # Velocity
        self.acceleration = [0.0,-9.81,0.0]     # Acceleration
        self.force = [0.0,0.0,0.0]              # Force
        self.dynamic = True                     # False -> Static body that does not move
        
        self.radius = 1                         # Body properties
        
        
    def step(self):
        self.current_step += 1
        self.time += self.h
        
        # Reset body forces
        for body in self.bodies:
            for i in range(len(body.acceleration)):
                body.force[i] = body.mass * body.acceleration[i]
        
        # Calculate forces, accelerations, and determine velocities
        for body1 in self.bodies:
            for body2 in self.bodies:
                if body1 != body2:
                    todo = True
                    # TODO: For each body pair, apply a force if they are colliding 
                   
                    x = body1.position[0] - body2.position[0]
                    y = body1.position[1] - body2.position[1]
                    distance = math.sqrt ((x)**2+(y)**2)- body1.radius - body2.radius
                    circleangle = math.atan2(y,x)
                    if distance <= 0:
                        body1.applyForce(math.atan2(y,x),300)
    
    def Draw(self):
          scale = 50
          for body in self.bodies:
            if body.type == "circle":
                X0 = self.window_width/2.0
                Y0 = self.window_height/2.0 
                x = scale*body.position[0]
                y = scale*body.position[1]
                r = scale*body.radius
                self.w.create_oval(X0+x-r,Y0-y-r,X0+x+r,Y0-y+r,fill=body.color)
                #self.w.create_line(X0+x-r,Y0-y-r, X0+x+r, Y0-y+r)
    def applyForce(self,direction,magnitude):
        self.force[0] += magnitude * math.cos(direction)
        self.force[1] += magnitude * math.sin(direction)
    
    def addBody(self, body):
        body.id = len(self.bodies)+1
        self.bodies.append(body)
        
        
    def updateBody(self,h):
        if self.dynamic:
            # Velocity 
            for i in range(len(self.velocity)):
                self.velocity[i] = self.velocity[i] + 0.5*(self.force[i]/self.mass)*h*h
    
            # Translation and rotation
            for i in range(len(self.position)):
                self.position[i] = self.position[i] + self.velocity[i]*h

    # Run the simulation
    def run(self):
        self.master = tk.Tk()
        self.w = tk.Canvas(self.master, width=self.window_width, height=self.window_height)
        self.w.pack()
        self.w.after(0,self.draw) 
        self.canvas = tk.Canvas(width = 592, height = 951, bg = 'black')
        self.gif1 = tk.PhotoImage(file = 'Background.gif')
        self.canvas.create_image(0, 0, image = self.gif1, anchor = tk.NW)
        self.canvas.pack()
        tk.mainloop()

        


program = MainClass()
program.run()


#canvas = tk.Canvas(width = 592, height = 951, bg = 'black')
#canvas.pack()

#canvas.create_image(50, 10, image = self.gif1, anchor = tk.NW)
